package com.sgb.zhaocai.view.demand.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.sgb.zhaocai.BR;
import com.sgb.zhaocai.R;
import com.sgb.zhaocai.entity.EquipmentListBase;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.DateUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Time:2021/9/18
 * AUTHOR:F_Q
 * Description:设备租赁
 */
public class BillEquipmentAdapter extends BaseRecycleAdapter<EquipmentListBase> {
    private Calendar dayCalendar = Calendar.getInstance();
    public BillEquipmentAdapter(Context context, List<EquipmentListBase> datas) {
        super(context, R.layout.zc_bill_equipment_items, datas);
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, @SuppressLint("RecyclerView") int position) {
        super.onBindViewHolder(holder, position);
        RelativeLayout delete = holder.itemView.findViewById(R.id.delete);//删除
        ItemInputTextView device_type = holder.itemView.findViewById(R.id.device_type);//设备分类
        ItemInputTextView device_name = holder.itemView.findViewById(R.id.device_name);//设备名称
        ItemInputTextView machine_model = holder.itemView.findViewById(R.id.machine_model);//规格型号
        ItemInputTextView metering_unit = holder.itemView.findViewById(R.id.metering_unit);//计量单位
        ItemInputTextView lease_num = holder.itemView.findViewById(R.id.lease_num);//采购量
        ItemInputTextView use_time = holder.itemView.findViewById(R.id.use_time);//使用时间
        editTextChanged(device_name.getContent_edt(), datas.get(position), position, 1);
        editTextChanged(machine_model.getContent_edt(), datas.get(position), position, 2);
        editTextChanged(metering_unit.getContent_edt(), datas.get(position), position, 3);
        editTextChanged(lease_num.getContent_edt(), datas.get(position), position, 4);
        device_type.setContent(datas.get(position).getCategoryName());//设备分类
        lease_num.setCheck(9,4);
        lease_num.setInfilterType(ItemInputTextView.TYPE_NUMBER_DECIMAL);
        //设备使用时间
        if (!TextUtils.isEmpty(datas.get(position).getPlanUseTime())){
            use_time.setContent(DateUtils.timestampToDate(Long.parseLong(datas.get(position).getPlanUseTime())));
        }else {
            use_time.setContent("");
        }
        //使用时间
        use_time.setOnWholeItemClickListener(new ItemInputTextView.OnWholeItemClickListener() {
            @Override
            public void OnWholeItemClick(View view) {
                pickTimeData(use_time, datas.get(position));
            }
        });
        //机械分类
        device_type.setOnWholeItemClickListener(new ItemInputTextView.OnWholeItemClickListener() {
            @Override
            public void OnWholeItemClick(View view) {
                onItemType.DeviceType(position);
            }
        });
        //删除
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemType.Delete(position);
            }
        });
    }

    @Override
    public void convert(ItemViewHolder holder, EquipmentListBase entity) {
        holder.setBinding(BR.viewModel, entity);
    }

    /**
     * 时间选择
     *
     * @param pick_time
     * @param equipmentlist
     */
    private void pickTimeData(ItemInputTextView pick_time, EquipmentListBase equipmentlist) {
        dayCalendar.setTime(new Date());
        dayCalendar.add(Calendar.DAY_OF_MONTH, 0);
        TimePickerView timePicker = new TimePickerBuilder(context, (date, v) -> {
            @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String format = sdf.format(date);
            equipmentlist.setPlanUseTime(String.valueOf(DateUtils.dateTo13Timestamp(format)));
            pick_time.setContent(format);
        }).setType(new boolean[]{true, true, true, false, false, false})
                .setCancelText("取消").setSubCalSize(15)
                .setCancelColor(Color.parseColor("#666666"))
                .setTitleText("请选择时间")
                .setTitleColor(Color.parseColor("#969696")).setTitleSize(15)
                .setSubmitText("确定")
                .setSubmitColor(Color.parseColor("#5792FD"))
                .setRangDate(dayCalendar, null)
                .build();
        timePicker.show();
    }

    private void editTextChanged(EditText editText, EquipmentListBase field, int pos, int type) {
        if (editText.getTag() != null) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
            editText.clearFocus();
        }
        switch (type) {
            case 1://材料名称
                editText.setText(field.getName());
                break;
            case 2://规格型号
                editText.setText(field.getSpec());
                break;
            case 3://计量单位
                editText.setText(field.getUnit());
                break;
            case 4://采购量
                editText.setText(field.getItemizedUseAmount());
                break;
        }

        TextWatcher watcher = new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                switch (type) {
                    case 1://机械名称
                        getDatas().get(pos).setName(s.toString());
                        break;
                    case 2://规格型号
                        getDatas().get(pos).setSpec(s.toString());
                        break;
                    case 3://计量单位
                        getDatas().get(pos).setUnit(s.toString());
                        break;
                    case 4://租赁量
                        getDatas().get(pos).setItemizedUseAmount(s.toString());
                        break;
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };
        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }

    private onItemType onItemType;

    public void setOnItemType(onItemType onItemType) {
        this.onItemType = onItemType;
    }

    public interface onItemType {
        void DeviceType(int pos);
        void Delete(int pos);
    }
}
